/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ep.m001.ui.views;

import com.vaadin.terminal.gwt.server.WebApplicationContext;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.ComponentContainer;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.LoginForm;
import com.vaadin.ui.LoginForm.LoginEvent;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Window;
import com.vaadin.ui.Window.Notification;
import com.vaadin.ui.themes.Reindeer;
import ep.libs.implementations.AbstractModule;
import ep.libs.implementations.AbstractView;
import ep.m001.ui.controllers.M001Controller;
import ep.m002.module.M002Module;

/**
 *
 * @author Spyros
 */
public class LoginView extends AbstractView {

    public static final String CREATE_ACCOUNT_CAPTION = "Δημιουργία Λογαριασμού";
    public static final String REGISTER_CAPTION = "Εγγραφή";
    public static final String LOGIN_CAPTION = "Σύνδεση";
    public static final String LOGOUT_CAPTION = "Αποσύνδεση";
    public static final String USERNAME_CAPTION = "Όνομα χρήστη";
    public static final String PASSWORD_CAPTION = "Κωδικός Πρόσβασης";
    public static final String EDITPROFILE_CAPTION = "Επεξεργασία Στοιχείων";
    public static final String ERROR_LOGIN ="Πιθανόν δώσατε λάθος Όνομα Χρήστη-Κωδικό Πρόσβασης ή δεν είστε εγγεγραμένο μέλος.";
    public static final Short DOCTOR_TYPE = 0;
    public static final Short PHARMACIST_TYPE = 1;
    public static final Short DEP_TYPE = 2;
    private M001Controller controller;

    public LoginView(AbstractModule module) {
        super(module);
    }

    public LoginView(AbstractModule module, Object[] args) {
        super(module, args);
    }

    @Override
    public ComponentContainer getView() {
        controller = new M001Controller();
        initializeComponents();
        return viewLayout;
    }

    private void initializeComponents() {
        LoginForm loginForm = new LoginForm();
        loginForm.setLoginButtonCaption(LOGIN_CAPTION);
        loginForm.setUsernameCaption(USERNAME_CAPTION);
        loginForm.setPasswordCaption(PASSWORD_CAPTION);
        loginForm.addListener(new LoginForm.LoginListener() {

            @Override
            public void onLogin(LoginEvent event) {
                Short loginType = controller.login(
                        event.getLoginParameter("username"),
                        event.getLoginParameter("password"));
                if (loginType != null) {
                    // add username to session
                    ((WebApplicationContext) module.getApplication().getContext()).getHttpSession().setAttribute("username", event.getLoginParameter("username"));
                    // change view
                    module.getApplication().changeModule(M002Module.class, new Object[]{loginType});
                    // add logout and edit profile
                    addLogoutButton(event.getLoginParameter("username"));
                }else{
                viewLayout.getWindow().showNotification(
                        ERROR_LOGIN,Notification.TYPE_WARNING_MESSAGE);
                }
            }
        });

        Button registerButton = new Button(CREATE_ACCOUNT_CAPTION);
        registerButton.addListener(new Button.ClickListener() {

            @Override
            public void buttonClick(ClickEvent event) {
                RegisterView registerView = new RegisterView(module, new Object[]{false});
                changeView(registerView);
            }
        });

        Panel loginPanel = new Panel(LOGIN_CAPTION);
        Panel registerPanel = new Panel(REGISTER_CAPTION);
        viewLayout.addComponent(loginPanel);
        viewLayout.addComponent(registerPanel);
        loginPanel.addComponent(loginForm);
        registerPanel.addComponent(registerButton);
    }

    public void addLogoutButton(String username) {
        Button logoutButton = new Button(LOGOUT_CAPTION);
        logoutButton.addListener(new Button.ClickListener() {

            @Override
            public void buttonClick(ClickEvent event) {
                controller.logout(module);
            }
        });
        logoutButton.setStyleName(Reindeer.BUTTON_LINK);
        Button editProfileButton = new Button(EDITPROFILE_CAPTION);
        editProfileButton.addListener(new Button.ClickListener() {

            @Override
            public void buttonClick(ClickEvent event) {
                RegisterView registerView = new RegisterView(module, new Object[]{true});
                Window editProfileWindow = new Window();
                editProfileWindow.setHeight("530px");
                editProfileWindow.setWidth("740px");
                editProfileWindow.setModal(true);
                editProfileWindow.setResizable(false);
                editProfileWindow.addComponent(registerView.getView());
                module.getApplication().getMainWindow().addWindow(editProfileWindow);
            }
        });
        Label usernameLabel = new Label(username);
        editProfileButton.setStyleName(Reindeer.BUTTON_LINK);
        HorizontalLayout headerLayout = module.getApplication().getHeaderLayout();
        headerLayout.addComponent(usernameLabel);
        headerLayout.setComponentAlignment(usernameLabel, Alignment.TOP_RIGHT);
        headerLayout.addComponent(logoutButton);
        headerLayout.setComponentAlignment(logoutButton, Alignment.TOP_RIGHT);
        headerLayout.addComponent(editProfileButton);
        headerLayout.setComponentAlignment(editProfileButton, Alignment.TOP_RIGHT);

    }
}
